<?php
/**
 * 
 * @author tangbl
 *
 */
class OrderAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 列表
	 */
    public function index(){ 
    	
    	if(IS_AJAX){
    		import('ORG.Util.Page');
    		$model     = D("Order");
    		
    		$orderDesc = $this->_get("orderDirection","trim","desc");;
    		$orderName = $this->_get("orderName","trim","addtime");
    		$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
    		$nowPage   = $this->_get("p","intval",1);
    		
    		$map       = unescape($this->_get("where"));
    		$map       = "1=1 ".$map;
    		$count     = $model->where($map)->count();
    		$Page      = new Page($count);
    		$show      = $Page->show();
    		$list      = $model->where($map)->order($orderName.' '.$orderDesc)
    					 ->page($nowPage.','.$pageNum)->select();
    		$totalPage = ceil($count/$pageNum);
    		C('web_admin_pagenum',$pageNum);
    		
    		$jsonData = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
    		$status0  = L('ord_form_ele_status0');
    		$status1  = L('ord_form_ele_status1');
    		foreach($list AS $row){
    			
    			$entry  = array('id'=>$row['id'],
    				'cell'=>array(
    					'id'         => $row['id'],
    					'orderno'    => $row['orderno'],
    					'addtime'    => $row['addtime'],
    					'totalfee'   => $row['totalfee'],
    					'paytime'    => $row['paytime'],
    					'username'   => $row['username'],
    					'email'      => $row['email'],
    					'status'     => get_array_key("ORDER_STATUS",$row['status']),
    					'lang'       => $row['lang'],
    					'addtime'    => date('Y-m-d H:i:s',$row['addtime']),
    					'deal'       => '
    				<a href="#" class="btn blue icn-only" onclick="selectOne('.$row['id'].')" title="'.L('ord_form_ele_select').'"><i class="icon-zoom-in"></i></a>
					<a href="#" class="btn red icn-only delAlone" title="'.L('cfg_list_itf_btn_delete').'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a> '
    				),
    			);
    			$jsonData['rows'][] = $entry;
    		}
    		$this->ajaxReturn($jsonData,'JSON');
    	}else{
    		$this->display();
    	}
    }
	
    /**
     * 订单详情
     */
	public function detail(){
		
		$id = $this->_param('id','intval',0);
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		//订单详情
		$order    = M("Order");
		$data   = $order->find($id);
		
		//订单操作记录
		$opreate      = D("OrderOpreateView");
		$m['orderno'] = $data['orderno'];
		$list         = $opreate->where($m)->order("addtime asc")->select();
		$this->assign("opreate",$list);
		
		//订单商品详情
		$m['userid'] = $data['userid'];
		$m['id']     = $id;
		$m['detail'] = 1;
		$json = sendRest("Order/userDetail",$m);
		$this->assign("detail",$json['data']);
		
		$this->display();	
	}
	
	/**
	 * 删除
	 */
	public function del(){
		
		parent::do_del("Order",true);
	}
}